package com.gin

import com.gin.pixiv.api.IllustrationApi
import com.gin.pixiv.callback.WriteFileCallback
import com.gin.pixiv.enums.WorkType
import com.gin.pixiv.interceptor.LogInterceptor
import com.gin.pixiv.main.Pixiv
import com.gin.pixiv.param.IllustLikeParam
import com.gin.pixiv.param.bookmark.BookmarkDelParam
import com.gin.pixiv.param.bookmark.IllustAddParam
import java.io.File

/**
 * @author bx002
 * @since 2024/2/18 10:51
 */
fun main() {
    val session = File("D:/Working/pixiv-cookie.txt").readText()
    val token = File("D:/Working/pixiv-token.txt").readText()

    val okHttpClient = Pixiv.obtainOkHttpClient(session, token).newBuilder()
        .addInterceptor(LogInterceptor())
        .build()
    val retrofit = Pixiv.obtainRetrofit(okHttpClient)
    val pixiv = Pixiv(retrofit)

    val pid = 99147997L

//    illustrationApi.getBookmarkData(99147997).enqueue(object : PixivCallback<BookmarkDataBody>() {
//        override fun onSuccess(body: BookmarkDataBody) {
//            println(Pixiv.MAPPER.writeValueAsString(body))
//        }
//    })

//    illustrationApi.getRecommendInit(116157584).enqueue(WriteFileCallback())
//    illustrationApi.getRecommends(setOf(116157584, 110035447, 110086719)).enqueue(WriteFileCallback())
//    testLike(illustrationApi)

//    pixiv.illustCommentApi().getRoots(99147997).enqueue(WriteFileCallback())
//    pixiv.illustCommentApi().getReplies(145996213).enqueue(WriteFileCallback())
//    pixiv.illustCommentApi().postAdd(
//        CommentType.stamp,
//        99147997,
//        20670838,
//        145996213,
//        null,
//        PixivStamp.STAMP_304
//    ).enqueue(WriteFileCallback())
//    pixiv.illustCommentApi().postDelete(99147997, 171930846).enqueue(WriteFileCallback())

//    val novelCommentApi = pixiv.novelCommentApi()
//    novelCommentApi.postAdd(
//        CommentType.stamp,
//        19024220,
//        17397118,
//        null,
//        null,
//        PixivStamp.STAMP_304
//    ).enqueue(WriteFileCallback())

//    novelCommentApi.getRoots(19024220).enqueue(WriteFileCallback())
//    novelCommentApi.postDelete(19024220, 46815489).enqueue(WriteFileCallback())

//    bookmarkApi.postAddIllust(param).enqueue(WriteFileCallback())

//    pixiv.novelApi().getSearch(keywords = "RO635", workLang = WorkLang.ZH_CN).enqueue(WriteFileCallback())
//    pixiv.illustrationApi().getSearch(keywords = "RO635").enqueue(WriteFileCallback())
//    pixiv.illustrationApi().getDetail(pid).enqueue(WriteFileCallback())
//    pixiv.novelApi().getDiscovery(20963352).enqueue(WriteFileCallback())
//    pixiv.novelApi().getRecommends(setOf(20963352, 17992560)).enqueue(WriteFileCallback())
//    pixiv.novelApi().getBookmarkData(20963352).enqueue(WriteFileCallback())
//    testBookmark(pid, pixiv);

//    pixiv.novelSeriesApi().getDetail(id = 10312007).enqueue(WriteFileCallback())
//    pixiv.novelSeriesApi().getTitles(id = 10312007).enqueue(WriteFileCallback())
//    pixiv.novelSeriesApi().getContents(id = 10312007, orderBy = PixivOrderBy.desc).enqueue(WriteFileCallback())
//    pixiv.rankingApi().getIllustrationRanking().enqueue(RawWriteFileCallback())

//    pixiv.tagApi().getSuggestByWord("RO635").enqueue(RawWriteFileCallback())
//    pixiv.tagApi().getTagDetail("RO635").enqueue(WriteFileCallback())
//    pixiv.tagApi().postIllustAdd(116408530, TagAddParam("RO635")).enqueue(WriteFileCallback())
//    pixiv.tagApi().postNovelAdd(21472339, TagAddParam("RO635")).enqueue(WriteFileCallback())

//    pixiv.novelApi().getDetail(21472339).enqueue(WriteFileCallback())

//    pixiv.userApi().getUserInfo(20670838, 1).enqueue(WriteFileCallback())
//    pixiv.userApi().getRecommend(20670838).enqueue(WriteFileCallback())

//    pixiv.bookmarkApi().postNovelDel(123).enqueue(WriteFileCallback())
//    pixiv.userApi().postFollow(20670838).enqueue(RawWriteFileCallback())

//    pixiv.userBookmarkApi().getBookmarkTags(WorkType.novel, 57680761).enqueue(WriteFileCallback())
//    pixiv.bookmarkQueryApi().getIllustrations(57680761).enqueue(WriteFileCallback())
//    pixiv.bookmarkQueryApi().getNovels(57680761).enqueue(WriteFileCallback())
//    pixiv.bookmarkManageApi()
//        .postAddTags(WorkType.novel, AddTagsParam(setOf(2061505455, 2061505308), setOf("少女前线")))
//        .postEditRestrict(WorkType.novel, EditRestrictParam(setOf(2079155061, 2068239152), BookmarkRestrict.public))
//        .postRenameTag(WorkType.novels, RenameTagParam("少女前线", ""))
//        .enqueue(WriteFileCallback())
//    pixiv.bookmarkQueryApi().getBookmarkTags(WorkType.novels, 57680761)
//    pixiv.userWorkApi().getUserWorkTags(FullWorkType.illusts, 20670838)
//    pixiv.userWorkApi().getProfileTop(93585002)
//    pixiv.userWorkApi().getProfileNovels(93585002, setOf(21674286))
//    pixiv.userWorkApi().getUserIllustsTag(20670838, "RO635(ドールズフロントライン)")
//    pixiv.userWorkApi().getUserNovelsTag(93585002, "HK416(少女前線)")
//    pixiv.commissionApi().getRequest(20670838)
    pixiv.commissionApi().getRequestSent(67898338)
        .enqueue(WriteFileCallback())


}

/**
 * 测试收藏相关功能
 */
fun testBookmark(pid: Long, pixiv: Pixiv) {
    // 查询收藏数据
    val illustApi = pixiv.illustrationApi()
    val bookmarkApi = pixiv.bookmarkManageApi()

    val response = illustApi.getBookmarkData(pid).execute()
    val bookmarkData = response.body()?.body?.bookmarkData
    if (bookmarkData == null) {
        // 添加 然后 删除
        println("作品未收藏, 执行收藏, pid: $pid")
        val res = bookmarkApi.postIllustAdd(IllustAddParam(pid)).execute()
        val lastBookmarkId = res.body()?.body?.lastBookmarkId
        println("收藏成功 bmkId: $lastBookmarkId")

        println("取消收藏, bmkId: $lastBookmarkId")
        bookmarkApi.postDeleteIllusts(lastBookmarkId!!).execute()
        println("取消成功, bmkId: $lastBookmarkId")
    } else {
        // 删除 然后 添加
        println("取消收藏, bmkId: $bookmarkData.id")
        bookmarkApi.postDelete(WorkType.illusts, BookmarkDelParam(setOf(bookmarkData.id!!))).execute()
        println("取消成功, bmkId: $bookmarkData.id")

        println("收藏作品, pid: $pid")
        val res = bookmarkApi.postIllustAdd(IllustAddParam(pid)).execute()
        val lastBookmarkId = res.body()?.body?.lastBookmarkId
        println("收藏成功 bmkId: $lastBookmarkId")
    }
}

fun testLike(api: IllustrationApi) {
    api.postLike(IllustLikeParam(103734378)).enqueue(WriteFileCallback())
}

